//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.action;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.mdiy.biz.IDictBiz;
import net.mingsoft.mdiy.entity.DictEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-自定义模块接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/mdiy/dict"})
public class DictAction extends BaseAction {
    @Autowired
    private IDictBiz dictBiz;

    public DictAction() {
    }

    @ApiIgnore
    @GetMapping({"/index"})
    public String index(HttpServletResponse response, HttpServletRequest request) {
        return "/mdiy/dict/index";
    }

    @ApiIgnore
    @GetMapping({"/form"})
    public String form(@ModelAttribute DictEntity dict, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        return "/mdiy/dict/form";
    }

    @ApiOperation("导入自定义字典")
    @LogAnn(
            title = "导入自定义字典",
            businessType = BusinessTypeEnum.INSERT
    )
    @PostMapping({"/importJson"})
    @ResponseBody
    @RequiresPermissions({"mdiy:dict:importJson"})
    public ResultData importJson(@RequestBody List<DictEntity> dictEntities, HttpServletResponse response, HttpServletRequest request, BindingResult result) {
        if (CollUtil.isEmpty(dictEntities)) {
            return ResultData.build().error("json格式不匹配");
        } else {
            List<DictEntity> list = this.dictBiz.list();
            List<DictEntity> dictEntityList = (List)dictEntities.stream().filter((dictEntity) -> {
                return !list.contains(dictEntity);
            }).map((dictEntity) -> {
                dictEntity.setId((String)null);
                return dictEntity;
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(dictEntityList)) {
                this.dictBiz.saveBatch(dictEntityList, dictEntityList.size());
            }

            return ResultData.build().success();
        }
    }

    @ApiOperation("查询字典表列表接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "dictType",
            value = "类型",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictLabel",
            value = "标签名",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictValue",
            value = "数据值",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "isChild",
            value = "子业务关联",
            required = false,
            paramType = "query"
    )})
    @RequestMapping(
            value = {"/list"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public ResultData list(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        BasicUtil.startPage();
        if (dict.getDictEnable() == null) {
            dict.setDictEnable(true);
        } else {
            dict.setDictEnable((Boolean)null);
        }

        List dictList = this.dictBiz.query(dict);
        return ResultData.build().success(new EUListBean(dictList, (int)BasicUtil.endPage(dictList).getTotal()));
    }

    @ApiOperation("根据子业务类型获取所有字典类型")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "isChild",
            value = "子业务关联",
            required = false,
            paramType = "query"
    )})
    @GetMapping({"/dictType"})
    @ResponseBody
    public ResultData dictType(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        BasicUtil.startPage();
        LambdaQueryWrapper<DictEntity> dictWrapper = new LambdaQueryWrapper();
        dictWrapper.select(DictEntity::getDictType);
        dictWrapper.groupBy(DictEntity::getDictType);
        List dictList = this.dictBiz.list(dictWrapper);
        return ResultData.build().success(new EUListBean(dictList, (int)BasicUtil.endPage(dictList).getTotal()));
    }

    @ApiOperation("根据字典类型获取字典，可支持多个类型用英文逗号隔开")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "dictType",
            value = "字典类型",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "isChild",
            value = "子业务关联",
            required = false,
            paramType = "query"
    )})
    @GetMapping({"/dictList"})
    @ResponseBody
    public ResultData dictList(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (StringUtils.isEmpty(dict.getDictType())) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("dict.type")}));
        } else {
            String[] types = dict.getDictType().split(",");
            DictEntity _dict = new DictEntity();
            _dict.setIsChild(dict.getIsChild());
            List list = new ArrayList();
            String[] var8 = types;
            int var9 = types.length;

            for(int var10 = 0; var10 < var9; ++var10) {
                String type = var8[var10];
                _dict.setDictType(type);
                list.add(this.dictBiz.query(_dict));
            }

            return ResultData.build().success(list);
        }
    }

    @ApiOperation("获取字典详情接口")
    @ApiImplicitParam(
            name = "id",
            value = "字典编号",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/get"})
    @ResponseBody
    public ResultData get(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (StringUtils.isBlank(dict.getId())) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("dict.id")}));
        } else {
            DictEntity _dict = (DictEntity)this.dictBiz.getById(dict.getId());
            return ResultData.build().success(_dict);
        }
    }

    @ApiOperation("保存字典接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "dictLabel",
            value = "标签名",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictType",
            value = "类型",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictValue",
            value = "数据值",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictDescription",
            value = "描述",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictSort",
            value = "排序（升序）",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "isChild",
            value = "子业务关联",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictRemarks",
            value = "备注信息",
            required = false,
            paramType = "query"
    )})
    @LogAnn(
            title = "保存字典接口",
            businessType = BusinessTypeEnum.INSERT
    )
    @PostMapping({"/save"})
    @ResponseBody
    @RequiresPermissions({"mdiy:dict:save"})
    public ResultData save(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request) {
        if (this.dictBiz.getByTypeAndLabelAndValue(dict.getDictType(), dict.getDictLabel(), (String)null) != null) {
            return ResultData.build().error(this.getResString("diy.dict.type.and.label.repeat"));
        } else if (this.dictBiz.getByTypeAndLabelAndValue(dict.getDictType(), (String)null, dict.getDictValue()) != null) {
            return ResultData.build().error(this.getResString("diy.dict.type.and.value.repeat"));
        } else {
            this.dictBiz.saveEntity(dict);
            if (StringUtil.isBlank(dict.getDictValue())) {
                dict.setDictValue(dict.getId());
                this.dictBiz.updateEntity(dict);
            }

            return ResultData.build().success();
        }
    }

    @ApiOperation("批量删除字典")
    @LogAnn(
            title = "批量删除字典",
            businessType = BusinessTypeEnum.DELETE
    )
    @PostMapping({"/delete"})
    @ResponseBody
    @RequiresPermissions({"mdiy:dict:del"})
    public ResultData delete(@RequestBody List<DictEntity> dicts, HttpServletResponse response, HttpServletRequest request) {
        int[] ids = new int[dicts.size()];

        for(int i = 0; i < dicts.size(); ++i) {
            ids[i] = Integer.parseInt(((DictEntity)dicts.get(i)).getId());
        }

        this.dictBiz.delete(ids);
        return ResultData.build().success();
    }

    @ApiOperation("更新字典信息接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "字典编号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictLabel",
            value = "标签名",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictType",
            value = "类型",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictValue",
            value = "数据值",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictDescription",
            value = "描述",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictSort",
            value = "排序（升序）",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "isChild",
            value = "子业务关联",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictRemarks",
            value = "备注信息",
            required = false,
            paramType = "query"
    )})
    @LogAnn(
            title = "更新字典信息接口",
            businessType = BusinessTypeEnum.UPDATE
    )
    @PostMapping({"/update"})
    @ResponseBody
    @RequiresPermissions({"mdiy:dict:update"})
    public ResultData update(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request) {
        DictEntity _dict = this.dictBiz.getByTypeAndLabelAndValue(dict.getDictType(), dict.getDictLabel(), (String)null);
        if (_dict != null && !_dict.getId().equals(dict.getId())) {
            return ResultData.build().error(this.getResString("diy.dict.type.and.label.repeat"));
        } else {
            DictEntity _dict2 = this.dictBiz.getByTypeAndLabelAndValue(dict.getDictType(), (String)null, dict.getDictValue());
            if (_dict2 != null && !_dict2.getId().equals(dict.getId())) {
                return ResultData.build().error(this.getResString("diy.dict.type.and.value.repeat"));
            } else {
                if (StringUtils.isBlank(dict.getDictValue())) {
                    dict.setDictValue((String)null);
                }

                this.dictBiz.updateEntity(dict);
                return ResultData.build().success();
            }
        }
    }

    @ApiOperation("刷新字典缓存接口")
    @PostMapping({"/updateCache"})
    @RequiresPermissions({"mdiy:dict:update"})
    @ResponseBody
    public ResultData updateCache(HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        this.dictBiz.updateCache();
        return ResultData.build().success();
    }
}
